package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.TrainClassDTO;
import com.yqg.education.controller.dto.questionDTO;
import com.yqg.education.entity.Question;
import com.yqg.education.mapper.QuestionMapper;
import com.yqg.education.service.IQuestionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * QuestionController 前端控制器
 * @author 张芸博
 * @since 2025-04-08
 */
@RestController
@RequestMapping("/question")
public class QuestionController {
@Resource
private QuestionMapper questionMapper;
    @Resource
    private IQuestionService questionService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody Question question) {
        question.setCreateTime(LocalDateTime.now());
        return Result.success(questionService.saveOrUpdate(question));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Question question =questionService.getById(id);
        question.setIsDelete(1);
        return Result.success(questionService.saveOrUpdate(question));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<Question> list=  questionService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(questionService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(questionService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    @GetMapping("pageList")
    public Result selectList(@RequestParam Integer pageNum,@RequestParam Integer pageSize, Integer courseId){
        pageNum = pageNum * pageSize - pageSize;
        List<Question> questionList = questionMapper.findPage(pageNum, pageSize, courseId);
        Long count = questionMapper.findCount(courseId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", questionList);
        map.put("total", count);
        return Result.success(map);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @Transactional(rollbackFor = Exception.class)
    public void importData(MultipartFile file) {
        questionService.importData(file);
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        questionService.export(response);
    }
}

